package com.lyfei.yd.system.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyfei.yd.system.domain.dto.SpecValueDto;
import com.lyfei.yd.system.domain.entity.*;
import com.lyfei.yd.system.mapper.SpecValueMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("specValueService")
public class SpecValueService extends BaseService<SpecValueMapper, SpecValue> {

    @Autowired
    private SkuSpecService skuSpecService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpecKeyService specKeyService;


    public SpecKeyValue getSpecKeyValue(Long specKeyId, Long specValueId){
        return this.baseMapper.getSpecKeyValue(specKeyId, specValueId);
    }

    public void create(SpecValueDto dto) {
        SpecValue specValue = new SpecValue();
        BeanUtils.copyProperties(dto, specValue);
        specValue.setCreateTime(new Date());
        specValue.setUpdateTime(new Date());
        this.save(specValue);
    }

    @Transactional
    public void update(SpecValueDto dto, Long id) {
        SpecValue specValue = this.getById(id);
        if(specValue == null) {
            throw new RuntimeException("规格值不存在");
        }

        BeanUtils.copyProperties(dto, specValue);
        specValue.setUpdateTime(new Date());
        this.updateById(specValue);

        // 更新Sku的specs字段
        List<SkuSpec> skuSpecs = skuSpecService.list(new LambdaQueryWrapper<SkuSpec>().eq(SkuSpec::getValueId, id));
        if(skuSpecs.isEmpty()) {
            return;
        }

        List<Long> skuIds = skuSpecs.stream().map(SkuSpec::getSkuId).collect(Collectors.toList());
        skuIds.forEach(skuId ->{
            Sku sku = skuService.getById(skuId);
            List<SkuSpec> tempSkuSpecs = skuSpecs
                    .stream()
                    .filter(skuSpec -> skuSpec.getSkuId().equals(skuId))
                    .collect(Collectors.toList());

            ArrayList<SpecKeyValue> specs = new ArrayList<>();
            tempSkuSpecs.forEach(skuSpec -> {
                SpecKeyValue specKeyValue = this.getSpecKeyValue(skuSpec.getKeyId(), skuSpec.getValueId());
                specs.add(specKeyValue);
            });
            sku.setSpecs(JSON.toJSONString(specs));
            skuService.updateById(sku);
        });
    }

    public void delete(Long id) {
        SpecValue specValue = this.getById(id);
        if(specValue == null) {
            throw new RuntimeException("规格值不存在");
        }

        // 删除规格值
        this.removeById(id);
    }

    public List<SpecValue> getByKeyId(Long keyId) {
        return this.list(new LambdaQueryWrapper<SpecValue>()
                .eq(SpecValue::getSpecId, keyId));
    }

}
